package se.webbzon.boltzmann.editor.component.sequence.panel;

import java.awt.GridLayout;
import java.util.Collection;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import se.webbzon.boltzmann.editor.component.JCombinedTextField;
import se.webbzon.boltzmann.game.object.dialogue.DialogueBackground;
import se.webbzon.boltzmann.game.sequence.builder.dialogue.AbstractDialogueRow;

public abstract class AbstractDialogueRowPanel extends AbstractRowPanel {

	// Auto generated serial version UID
	private static final long serialVersionUID = 8461589407901466837L;
	
	// The maximal number of characters per row
	private static final int lineMaxLength = 35;

	// The combo boxes of this panel
	private JComboBox<DialogueBackground> backgroundInput;
	
	// The text fields of this panel
	private JCombinedTextField[] textInputs;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AbstractDialogueRowPanel(AbstractDialogueRow row) {
		this(row,3);
	}
	
	public AbstractDialogueRowPanel(AbstractDialogueRow row, int rows) {
		super(row);
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		// Create default panel
		JPanel defaultPanel = new JPanel();
		defaultPanel.setLayout(new GridLayout(1 + rows,2));
		
		// Distribute the text among the text fields
		textInputs = new JCombinedTextField[rows];
		String[] lines = row.getText().split("#");
		for (int i = 0; i < rows; i++) {
			final String text;
			if (i < lines.length) {
				if (i == rows-1) {
					// Add the rest of the text to the last row
					StringBuilder builder = new StringBuilder();
					for (int j = i; j < lines.length; j++)
						builder.append(lines[j] + "#");
					text = builder.toString();
				} else	
					// Add the line to the text fields
					text = lines[i] + "#";
			}else
				// Add empty row to text fields
				text = "";
			
			defaultPanel.add(new JLabel("Text (line " + (i+1) + "):"));
			textInputs[i] = new JCombinedTextField(text,lineMaxLength);
			defaultPanel.add(textInputs[i]);
		}
		
		defaultPanel.add(new JLabel("Background:"));
		backgroundInput = 
				new JComboBox<DialogueBackground>(DialogueBackground.values());
		backgroundInput.setSelectedItem(row.getBackground());
		defaultPanel.add(backgroundInput);
		
		add(defaultPanel);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Adds a panel at the end of the abstract dialogue row panel. **/
	public void addPanel(JPanel panel) {
		add(panel);
	}

	@Override public boolean apply(Collection<String> errorMessages) {
		AbstractDialogueRow row = (AbstractDialogueRow) getSequenceRow();
		
		// Update dialogue row
		StringBuilder builder = new StringBuilder();
		for (JCombinedTextField textInput : textInputs)
			builder.append(textInput.getText());
		row.setText(builder.toString());
		DialogueBackground background = 
				(DialogueBackground) backgroundInput.getSelectedItem();
		row.setBackground(background);
		
		return true;
	}
	
	

}
